<?php

session_start();
include_once 'classControleur.php';
include_once '../model/model_sql.php';
$captchatest = false;
//var_dump($_POST);
if (!empty($_POST)) {
    $nom = "";
    if (isset($_POST['nom'])) {
        $nom = $_POST['nom'];
    }
    $prenom = "";
    if (isset($_POST['prenom'])) {
        $prenom = $_POST['prenom'];
    }
    $dateNaissance = "";
    if (isset($_POST['dateNaissance'])) {
        $dateNaissance = $_POST['dateNaissance'];
    }
    $email = "";
    if (isset($_POST['email'])) {
        $email = $_POST['email'];
    }
    $password = "";
    if (isset($_POST['password'])) {
        $password = $_POST['password'];
    }

    if (isset($_POST['captcha'])) {
        
		if($_POST['captcha'] != $_SESSION['digit'])
				$captchatest = true;
    }

    $SQLBlog = new SQLBlog();
    $controleur = new classControleur();
    $messageEnom = $controleur->testNom($nom);
    $messageEprenom = $controleur->testNom($prenom);
    $messageEemail = $controleur->testEmail($email);
    $messageEpassword = $controleur->testPassword($password);
	$date=str_replace('/','-',$dateNaissance);
    $dateDeNaissanceFormat = date("Y-m-d", strtotime($date));
    if ($controleur->getErreur() == false  && !$captchatest) {
        $SQLBlog->ajouterUtilisateur($email, $password, $nom, $prenom, $dateDeNaissanceFormat);
        header("Location:../vue/index.php",10000);
    } else {
        header("Location:../vue/FormulaireInscription.php?messageEnom=$messageEnom&messageEprenom=$messageEprenom&messageEemail=$messageEemail&messageEpassword=$messageEpassword&nom=$nom&prenom=$prenom&dateDeNaissance=$dateNaissance&email=$email");
    }
}
?>

